package com.interfaces.wx.pay;

import com.interfaces.module.order.domain.FtsOrder;
import com.interfaces.util.TokenUtil;
import com.interfaces.wx.WxPayInit;
import com.ruoyi.common.utils.StringUtils;
import com.wechat.pay.java.service.payments.jsapi.model.*;
import com.wechat.pay.java.service.payments.model.Transaction;
import com.wechat.pay.java.service.refund.model.CreateRequest;
import com.wechat.pay.java.service.refund.model.Refund;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
@Component
public class WxJsapiService {
    public static String outTradeNoGen(){
        /****************订单号生成*******************/
        Long times=new Date().getTime();
        LocalDateTime now = LocalDateTime.now();
        // 创建一个DateTimeFormatter实例，指定日期和时间的格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyMMddHHmmss");
        // 使用formatter格式化当前日期和时间
        String formattedDateTime = now.format(formatter);
        StringBuffer outTradeNoStr=new StringBuffer("wx");
        outTradeNoStr.append(formattedDateTime);
        String timesStr=times.toString();
        String timesStr6=timesStr.substring(timesStr.length()-6,timesStr.length());
        outTradeNoStr.append(timesStr6);
        String outTradeNo=outTradeNoStr.toString();
        /***********************************/
        return outTradeNo;
    }
    /***
     * 支付下单与微信进行通讯
     */
    public static PrepayWithRequestPaymentResponse prepay(FtsOrder ftsOrder){
        PrepayRequest request = new PrepayRequest();
        request.setAppid(WxPayInit.APPID);
        request.setMchid(WxPayInit.merchantId);
        Payer payer=new Payer();
        String openid=TokenUtil.getTokenUser().getOpenId();
        payer.setOpenid(openid);
        request.setPayer(payer);
        Amount amount=new Amount();
        //传入的价格单位，在微信是按分算，所以乘100
        int priceTotal=(ftsOrder.getPrice().multiply(new BigDecimal(100)).setScale(2)).intValue();
        amount.setTotal(priceTotal);
        amount.setCurrency("CNY");
        request.setAmount(amount);
        String description="福天寿微信公众号商品"+ftsOrder.getOrderCode();
        request.setDescription(description);
        request.setOutTradeNo(ftsOrder.getOrderCode());
        request.setNotifyUrl(WxPayInit.NOTIFYURL);
        // 调用request.setXxx(val)设置所需参数，具体参数可见Request定义
        // 调用接口
        return WxPayInit.service.prepayWithRequestPayment(request);
    }


    /** 关闭订单 */
    public static boolean closeOrder(String OutTradeNo) {
        try{
            CloseOrderRequest request = new CloseOrderRequest();
            // 调用request.setXxx(val)设置所需参数，具体参数可见Request定义
            request.setMchid(WxPayInit.merchantId);
            request.setOutTradeNo(OutTradeNo);
            // 调用接口
            WxPayInit.service.closeOrder(request);
            return true;
        }catch (Exception e){
            return false;
        }

    }

    /**
     * 微信支付订单号查询订单
     * */
    public static Transaction queryOrderById(String transactionId) {

        QueryOrderByIdRequest request = new QueryOrderByIdRequest();
        // 调用request.setXxx(val)设置所需参数，具体参数可见Request定义
        request.setTransactionId(transactionId);
        request.setMchid(WxPayInit.merchantId);
        // 调用接口
        return WxPayInit.service.queryOrderById(request);
    }

    /**
     * 商户订单号查询订单
     * */
    public static Transaction queryOrderByOutTradeNo(String outTradeNo) {

        QueryOrderByOutTradeNoRequest request = new QueryOrderByOutTradeNoRequest();
        // 调用request.setXxx(val)设置所需参数，具体参数可见Request定义
        request.setOutTradeNo(outTradeNo);
        request.setMchid(WxPayInit.merchantId);
        // 调用接口
        return WxPayInit.service.queryOrderByOutTradeNo(request);
    }

    public static void refund(CreateRequest request){
        Refund  refund= WxPayInit.refundService.create(request);
    }

}
